﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Loogn.OrmLite;
using Stardust.Common.Model;
using Stardust.ConfigCenterWeb.Code.Entities;
using System.Text;

namespace Stardust.ConfigCenterWeb.Code.Repositories.Impl
{
    public class NodeEventRepository : INodeEventRepository
    {
        public long Add(NodeEvent m)
        {
            using (var db = DB.Open())
            {
                var flag = db.Insert(m);
                return flag;
            }
        }

        public long GetMaxId()
        {
            using (var db = DB.Open())
            {
                var id = db.MaxID<long>("NodeEvent");
                return id;
            }
        }

        public List<NodeEvent> GetNewList(long myMaxId, List<long> nodeIds)
        {
            using (var db = DB.Open())
            {
                var sql = "select * from NodeEvent where Id>" + myMaxId;
                if (nodeIds != null && nodeIds.Count > 0)
                {
                    sql += " and ServerNodeId in (" + string.Join(",", nodeIds) + ")";
                }
                var list = db.Select<NodeEvent>(sql);
                if (list.Count > 0)
                {
                    var serverNodeIds = list.Select(x => x.ServerNodeId);
                    var nodeList = db.SelectByIds<ServerNode>(serverNodeIds);
                    foreach (var item in list)
                    {
                        item.ServerNode = nodeList.First(x => x.Id == item.ServerNodeId);
                    }
                }
                return list;
            }
        }

        public List<NodeEvent> GetNodesUpdates(List<GetNodesUpdateParams> ps)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from NodeEvent where 1=2");
            foreach (var item in ps)
            {
                sb.AppendFormat(" or ( Id>{0} and ServiceName='{1}')", item.MaxEventId, SqlInjection.Filter(item.ServiceName));
            }

            using (var db = DB.Open())
            {
                var list = db.Select<NodeEvent>(sb.ToString());
                if (list.Count > 0)
                {
                    var serverNodeIds = list.Select(x => x.ServerNodeId);
                    var nodeList = db.SelectByIds<ServerNode>(serverNodeIds);
                    foreach (var item in list)
                    {
                        item.ServerNode = nodeList.FirstOrDefault(x => x.Id == item.ServerNodeId);
                        if (item.ServerNode == null)
                        {
                            item.ServerNode = new ServerNode
                            {
                                Id = item.ServerNodeId,
                                ServiceName = item.ServiceName,
                                Status = Common.Enum.ServerNodeStatus.Disabled
                            };
                        }
                    }
                }
                return list;
            }
        }
    }
}